package http.HttpUtil;

import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;

import org.apache.http.HttpHost;

import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ApnUtil {
	public static final String POST = "POST";
	public static final String GET = "GET";
	public static int proxyMode = 0;

	public static byte[] receiveHttpResponse(byte[] bytes) throws Exception {
		if ((bytes == null) || (bytes.length == 0))
			return null;
		// return TextUtil.decompressAfterdecrypt(TextUtil.CancelHead(bytes));
		return bytes;
	}

	/**
	 * 鍒ゆ柇鏄惁涓簑ifi
	 * 
	 * @param aContext
	 * @return
	 */
	public static boolean isWifi(Context aContext) {
		ConnectivityManager cm = (ConnectivityManager) aContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo network_info = cm.getActiveNetworkInfo();
		if ((network_info != null) && (network_info.isConnected())) {
			return network_info.getType() == 1;
		}
		return false;
	}

	public static String getAccessPoint(Context context) {
		return "";
//		return PhoneInfo.getAccessPoint(context);
	}

	public static Proxy getApnProxy(Context context) {

		// String apn = PhoneInfo.getAccessPoint(context);
		// System.out.println("閲岄潰:" + apn);
		//
		// String proxy = null;
		// String port = null;
		//
		// if (apn.equals("cmwap") || apn.equals("uniwap") ||
		// apn.equals("3gwap")) {
		// proxy = "10.0.0.172";
		// port = "80";
		// } else if (apn.equals("ctwap")) {
		// proxy = "10.0.0.200";
		// port = "80";
		// }
		//
		// System.out.println("ip:" + proxy);
		//
		// if ((proxy != null) && (!proxy.equals(""))) {
		// if (validateIp(proxy)) {
		// return new Proxy(Proxy.Type.HTTP,
		// InetSocketAddress.createUnresolved(proxy, Integer
		// .valueOf(port).intValue()));
		// }
		//
		// return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy,
		// Integer.valueOf(port).intValue()));
		// }

		NetworkInfo ni = ((ConnectivityManager) context.getApplicationContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

		if (ni.isConnected()) {
			return null;
		}

		Cursor c = getCurrentApn(context);
		if (c.getCount() > 0) {
			c.moveToFirst();
			long id = c.getLong(c.getColumnIndex("_id"));

			String proxy = c.getString(c.getColumnIndex("proxy"));
			String port = c.getString(c.getColumnIndex("port"));

			c.close();

			if ((proxy != null) && (!proxy.equals(""))) {
				if (validateIp(proxy)) {
					return new Proxy(Proxy.Type.HTTP,
							InetSocketAddress.createUnresolved(proxy, Integer
									.valueOf(port).intValue()));
				}

				return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy,
						Integer.valueOf(port).intValue()));
			}
		}

		c.close();

		return null;
	}

	public static boolean validateIp(String aIp) {
		String IPADDRESS_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

		Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
		Matcher matcher = pattern.matcher(aIp);
		return matcher.matches();
	}

	public static HttpHost getProxyToHttpClient(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getNetworkInfo(1);
		if (ni.isConnected()) {
			return null;
		}

		Cursor c = getCurrentApn(context);
		if (c.getCount() > 0) {
			c.moveToFirst();
			long id = c.getLong(c.getColumnIndex("_id"));

			String proxy = c.getString(c.getColumnIndex("proxy"));
			String port = c.getString(c.getColumnIndex("port"));
			c.close();

			if ((proxy != null) && (!proxy.equals(""))) {
				if ((port != null) && (!port.equals(""))) {
					return new HttpHost(proxy, Integer.parseInt(port));
				}

				return null;
			}
		}

		c.close();

		return null;
	}

	public static Cursor getCurrentApn(Context context) {
		return context.getContentResolver().query(
				Uri.parse("content://telephony/carriers/preferapn"), null,
				null, null, null);
	}

	public static HttpURLConnection getConnection(String url, Context context) {
		try {
			URL url_ = new URL(url);
			HttpURLConnection connection = null;
			Proxy proxy = null;
			if ((proxy = getApnProxy(context)) != null) {
				connection = (HttpURLConnection) url_.openConnection(proxy);
			} else {
				connection = (HttpURLConnection) url_.openConnection();
			}
			return connection;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}